package com.yvtian.inv.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yvtian.inv.pojo.InvInStockEntity;
import com.yvtian.inv.pojo.InvProductionAcceptanceEntity;
import com.yvtian.inv.service.InvInStockService;
import com.yvtian.annotation.Log;
import com.yvtian.entity.Result;
import org.springframework.web.bind.annotation.*;
import com.yvtian.entity.StatusCode;
import com.yvtian.enums.OperatorType;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;


/**
 * 生产入库单(inv_in_stock)表控制层
 *
 * @author long
 * @since 2024-07-02 13:43:34
 */
@RestController
@RequestMapping("/api/inv/inStock")
public class InvInStockController {
    /**
     * 服务对象
     */
    @Resource
    private InvInStockService invInStockService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return new Result(true, "查询成功", StatusCode.OK, this.invInStockService.getById(id));
    }

    /**
     * 通过编码查询单条数据
     *
     * @param code 唯一编码
     * @return 单条数据
     */
    @GetMapping("/code/{code}")
    public Result selectOneByCode(@PathVariable String code) {
        LambdaQueryWrapper<InvInStockEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvInStockEntity::getCode, code);
        return new Result(true, "查询成功", StatusCode.OK, this.invInStockService.getOne(queryWrapper));
    }

    @GetMapping(value = "/list")
    public Result findByPage(Page<InvInStockEntity> page, InvInStockEntity invInStockEntity) {
        return new Result(true, "查询成功", StatusCode.OK, invInStockService.findByPage(page, invInStockEntity));
    }

    /**
     * 新增数据
     *
     * @param invInStockEntity 实体对象
     * @return 新增结果
     */
    @Log(value = "添加数据", isSave = true, operType = OperatorType.INSERT)
    @PostMapping("insert")
    public Result insert(@RequestBody InvInStockEntity invInStockEntity) {
        return new Result(true, "插入成功", StatusCode.OK, this.invInStockService.save(invInStockEntity));
    }

    /**
     * 修改数据
     *
     * @param invInStockEntity 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(@RequestBody InvInStockEntity invInStockEntity) {
        return new Result(true, "更新成功", StatusCode.OK, this.invInStockService.updateById(invInStockEntity));
    }

    /**
     * 更新或保存数据
     * @param invInStockEntity 实体对象
     * @return 操作结果
     */
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody InvInStockEntity invInStockEntity) {
        return new Result(true, "操作成功", StatusCode.OK, this.invInStockService.saveOrUpdate(invInStockEntity));
    }

    /**
     * 删除数据
     *
     * @param idMap 主键结合
     * @return 删除结果
     */
    @PostMapping("/del")
    public Result delete(@RequestBody Map<String, ArrayList<Integer>> idMap) {
        ArrayList<Integer> ids = idMap.get("ids");
        LambdaUpdateWrapper<InvInStockEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(InvInStockEntity::getId, ids);
        lambdaUpdateWrapper.set(InvInStockEntity::getIsDel, 1);
        return new Result(true, "删除成功", StatusCode.OK, this.invInStockService.update(lambdaUpdateWrapper));
    }


    /**
     * 验收入库(审核自动入库)
     *
     * @param invInStockEntity 实体对象
     * @return 修改结果
     */
    @PostMapping("updateQuantity")
    public Result updateQuantity(@RequestBody InvInStockEntity invInStockEntity) {
        return new Result(true, "入库成功", StatusCode.OK, this.invInStockService.handleStorage(invInStockEntity));
    }
}

